home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / vmenu.arc / VMENU.C < prev    next >
Encoding:
Text File  |  1986-05-04  |  7.7 KB  |  203 lines

  1. /**************************************************************************/
  2. /*           Universal Vertical Menu      11:03:45  5/3/1986              */
  3. /*                                                                        */
  4. /*                  courtesy of Dr. Bob's Utilities                       */
  5. /*                                                                        */
  6. /*         This function prints a standard vertical menu on the screen    */
  7. /*    hilighting the current option in various ways.  It lets you         */
  8. /*    select the menu choice by number or by using the arrow or           */
  9. /*    home and end keys.                                                  */
  10. /*         It beeps at unacceptable key choices and returns the           */
  11. /*    integer value of the final selection.                               */
  12. /*                                                                        */
  13. /*    INPUT - the function is sent four arguments:                        */
  14. /*                                                                        */
  15. /*    1.  The title of the menu - char *title                             */
  16. /*    2.  A pointer to the array of choices - char *options[]             */
  17. /*        note that all choices have at least to leading and              */
  18. /*        trailing blanks and that all are the same length.               */
  19. /*        If this seems too wasteful to you, you can modify the code.     */
  20. /*        Also note that the array must end with a null string.           */
  21. /*                                                                        */
  22. /*    3.  Row of the upper left corner of the menu - int row, col         */
  23. /*    4.  Column of the upper left corner of menu.                        */
  24. /*        If row and col are both 0, the function automatically           */
  25. /*        centers the menu on the screen.                                 */
  26. /*                                                                        */
  27. /*    FUNCTIONS YOU MAY NOT HAVE:                                         */
  28. /*                                                                        */
  29. /*    1.  cursoff() - turns cursor off. Not necessary but improves look.  */
  30. /*    2.  smallcurs() - turns cursor back on.  Both included.             */
  31. /*    3.  cls() - clears screen, homes cursor.                            */
  32. /*    4.  locate() - places cursor.  (N.B. 1,1 is upper left corner)      */
  33. /*    5.  inch() - waits for and returns key from keyboard.               */
  34. /*                 included - calls kb() also included.                   */
  35. /*    6.  boop() - inoffensive beep,  included.                           */
  36. /*    7.  box()- draws a box, I'd give it to you but I don't own it.      */
  37. /*        box(ul_row,ul_col,lr_row,lr_col)                                */
  38. /*        hint: If you write one, draw the box clockwise from the upper   */
  39. /*              left corner.  Do the corners as you come to them,         */
  40. /*              if you wait and do them at the end it will look           */
  41. /*              dopey.                                                    */
  42. /*                                                                        */
  43. /*    FUNCTIONS YOU MAY WANT TO MODIFY:                                   */
  44. /*                                                                        */
  45. /*    1.  printf() - My printf function uses the int attr to determine    */
  46. /*        color and characteristics.  I don't own it so I can't give it   */
  47. /*        to you.  If yours doesn't do this you can modify it (or write   */
  48. /*        a substitute function).  You can also just use the code as      */
  49. /*        is since the asterisks will still hilight the current choice.   */
  50. /*        The menu function redraws the whole menu each time a key is     */
  51. /*        pressed. My printf() is in assembly language so this is not     */
  52. /*        noticable.  If yours is slow, you may want to rewrite the       */
  53. /*        function to minimize redraws.                                   */
  54. /*                                                                        */
  55. /*        Courtesy of:  Dr. Bob's Utilities                               */
  56. /*                      444 Maple Lane                                    */
  57. /*                      St. Paul MN  55126                                */
  58. /*                                                                        */
  59. /*        You can find Bob Ray on Terrapin Station BBS!                   */
  60. /*        A Programmer's BBS - 612/623-0152                               */
  61. /*        1200/2400  24 hours a day!                                      */
  62. /*                                                                        */
  63. /*                                                                        */
  64. /**************************************************************************/
  65.  
  66.  
  67. #include keys.h
  68.  
  69. #define test 1
  70.  
  71.  
  72. #define ON  1    /* use 1 for underline, 112 for inverse,  */
  73.                  /*   15 for hilight, 135 for blink        */
  74.  
  75. #define OFF 7    /* normal  */
  76.  
  77. #ifdef test
  78. int attr = 7;
  79.  
  80. main()
  81. {
  82.     int choice;
  83.     char temp[20];
  84.     static char *options[] = {
  85.         "  1. Menu choice number one    ",
  86.         "  2. Menu choice number two    ",
  87.         "  3. Menu choice number three  ",
  88.         "  4. Exit the program          ",
  89.         ""
  90.     };
  91.  
  92.  
  93.     choice = umenu("MAIN MENU",options,0,0);
  94.  
  95.     locate(20,5);
  96.     printf("You picked %d\n",choice);
  97.  
  98. }
  99. #endif;
  100.  
  101. umenu(title,options,row,col)
  102. char *title;
  103. char *options[];
  104. int row, col;
  105.  
  106. {
  107.     extern int attr;
  108.     int count;
  109.     int maxlen;
  110.     int i;
  111.     int active;
  112.     int done;
  113.     int ch;
  114.  
  115.  
  116.     count = 0;
  117.     active = 0;
  118.  
  119.     cursoff();
  120.  
  121.     while(*options[++count])   /* count number of menu choices  */
  122.         ;
  123.  
  124.     maxlen = strlen(options[1]);
  125.  
  126.     printf("\nMaxlen = %d\n",maxlen);
  127.     cls();
  128.  
  129.     if ( (row + col) == 0) {  /* center the menu  automatically */
  130.         row = 13 - (count/2);
  131.         col = 41 - (maxlen/2);
  132.     }
  133.  
  134.     locate(row-2,col - 4 + (maxlen/2) );
  135.     printf("%s",title);
  136.  
  137.     box(row-4,col-2,row+count+1,col + maxlen + 2);
  138.     done = 0;
  139.  
  140.     do {
  141.         for (i = 0; i < count; i++) {
  142.             locate(row + i,col);
  143.             if (i == active)
  144.                 attr  = ON;
  145.             else attr = OFF;
  146.  
  147.             printf("%s",options[i]);
  148.             if(i == active) {
  149.                  printf("\b*");
  150.                  locate(row+i,col);
  151.                  printf("*");
  152.             }
  153.  
  154.         }
  155.             ch = inch();
  156.                 if ( (ch >= '1') && (ch <= ('0' + count) ) ) {
  157.                     active = ch - '1';
  158.                     continue;
  159.                 }
  160.             switch(ch) {
  161.                 case('\r'):
  162.                 case ('q'):  /* SAFETY FIRST - REMOVE THIS IF CR works */
  163.                     done = 1;
  164.                     break;
  165.                 case HM:
  166.                     active = 0;
  167.                     break;
  168.                 case END:
  169.                     active = count -1;
  170.                     break;
  171.                 case DN:
  172.                 case RIGHT:
  173.                     if(active == count-1)
  174.                         active = 0;
  175.                     else
  176.                         active++;
  177.                     break;
  178.  
  179.                 case UP:
  180.                 case LEFT:
  181.                     if(active == 0)
  182.                         active = count - 1;
  183.                     else
  184.                         active--;
  185.                     break;
  186.  
  187.  
  188.  
  189.                 default:
  190.                     boop();
  191.                     break;
  192.  
  193.             }
  194.  
  195.     }  while ( !done);
  196.  
  197.     smallcurs();
  198.     attr = OFF;
  199.     return(active +1);
  200.  
  201. }
  202.  
  203.